package cn.com.checne.controller.superManager;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.com.checne.constants.Constants;
import cn.com.checne.constants.LogConstants;
import cn.com.checne.constants.NavigateConstants;
import cn.com.checne.dao.LogInfoDao;
import cn.com.checne.dao.UserInfoDao;
import cn.com.checne.model.domain.RightInfo;
import cn.com.checne.model.domain.UserInfo;
import cn.com.checne.util.StringTools;
import cn.com.checne.util.WebUtils;

@Controller
public class LoginManagerController{
	protected static final Logger logger = Logger.getLogger(LoginManagerController.class);

	@Resource
	private UserInfoDao userInfoDao;

	@Resource
	private LogInfoDao logDao ;
	
	/**
	 * 后台管理登陆界面
	 */
	@RequestMapping(value="/manager.do", method = RequestMethod.GET)
	public ModelAndView manager(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView("login");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0L);
		modelMap.put("message", "");
		mav.addAllObjects(modelMap);
		return mav;
	}
	
	/**
	 * 后台用户登陆验证
	 */
	@RequestMapping(value="/login.do",method = RequestMethod.POST)
	public ModelAndView adminLogin(HttpServletRequest request,HttpServletResponse response, HttpSession session, ModelMap modelMap) {
		String message = "";
		String viewName = "login";
		ModelAndView mav = new ModelAndView();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		int languageId = WebUtils.getRequestParameterAsInt(request, "languageId", 1);
		String verifyCode = request.getParameter("verifyCode");
		String code = (String) session.getAttribute("verifyCode");

		if ((StringUtils.isBlank(verifyCode)) || (!StringUtils.equals(verifyCode, code))) {
			message = "验证码错误!";
		} else if ((StringUtils.isNotBlank(username))
				&& (StringUtils.isNotBlank(password))) {
			
			//检查用户登陆
			UserInfo userInfo = userInfoDao.userLogin(username, password);
			if (userInfo == null) {
				message = "登录失败!";
			} else {
				request.getSession().setAttribute(Constants.SESSIONLANGUAGE,String.valueOf(languageId));
				request.getSession().setAttribute(Constants.SESSIONNAME, userInfo);
				List<RightInfo> riList = userInfo.getRoleInfo().getRights();
				List<String> pg = new ArrayList<String>();
				for (RightInfo r : riList) {
					pg.add(r.getRightValue());
				}
				request.getSession().setAttribute(Constants.SESSIORIGHTLIST, pg);
				modelMap.put("userInfo", userInfo);
				message = StringTools.getUUID();
				viewName = "redirect:/super/index.do";
				
				logDao.create(userInfo.getId(),LogConstants.ACTION_LOGIN, NavigateConstants.NAVI_SYSTEM, userInfo.getRealName() + "登入系統");
			}
		} else {
			message = "输入不能为空!";
		}

		modelMap.put("message", message);
		mav.setViewName(viewName);
		mav.addAllObjects(modelMap);
		return mav;
	}

	@RequestMapping(value = "/super/index.do", method = RequestMethod.GET)
	public String adminIndex() {
		return "super/index";
	}

	@RequestMapping(value = "/super/frame/top.do", method = RequestMethod.GET)
	public String top() {
		return "super/frame/top";
	}

	@RequestMapping(value = "/super/frame/index.do", method = RequestMethod.GET)
	public String frameIndex() {
		return "super/frame/frame_index";
	}

	@RequestMapping(value = "/super/index/left.do", method = RequestMethod.GET)
	public String indexLeft() {
		return "super/index/left";
	}

	@RequestMapping(value = "/super/index/webInfo.do", method = RequestMethod.GET)
	public String webInfo() {
		return "super/index/webInfo";
	}

	@RequestMapping(value = "/super/logout.do", method = RequestMethod.GET)
	public String logout(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		UserInfo userInfo = (UserInfo) request.getSession().getAttribute(Constants.SESSIONNAME);
		logDao.create(userInfo.getId(),LogConstants.ACTION_LOGIN, NavigateConstants.NAVI_SYSTEM, userInfo.getRealName() + "登出系統");
		session.removeAttribute(Constants.SESSIORIGHTLIST);
		session.removeAttribute(Constants.SESSIONNAME);
		session.invalidate();
		return "login";
	}
	
	@RequestMapping(value = "/super/changeLanguage.do", method = RequestMethod.GET)
	public String changeLanguage(HttpServletRequest request,HttpServletResponse response, HttpSession session) {
		int languageId = WebUtils.getRequestParameterAsInt(request, "languageId", 1);
		session.removeAttribute(Constants.SESSIONLANGUAGE);
		session.setAttribute(Constants.SESSIONLANGUAGE,String.valueOf(languageId));
		return "super/frame/frame_index";
	}
}